package tommysmapeditor.helper;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;

import tommysmapeditor.input.MapEditorInput;



/**
 * various utility methods for save and export
 * 
 * @author tommy
 *
 */
public class ExportHelper {
	public static void saveTileMatrixFile(String filePath,int tileWidth, int tileHeight, int[][] tileGrid) {
		FileWriter saveFile = null;
		try {
			saveFile = new FileWriter(filePath);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		BufferedWriter bw = new BufferedWriter(saveFile);
		
		String tileWidthToWrite ="";
		tileWidthToWrite+=tileWidth;

		String tileHeightToWrite ="";
		tileHeightToWrite+=tileHeight;
		
	
		
		try {
			bw.write(tileWidthToWrite);
			bw.newLine();
			bw.write(tileHeightToWrite);
			bw.newLine();

			
			for(int i=0;i<tileHeight;i++){
				String line= "";
				for(int j=0;j<tileWidth;j++ ){
					line+=tileGrid[j][i]+";";
				}
				bw.write(line);
				bw.newLine();
				bw.flush();
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			saveFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	
	/**
	 * saves a definition of tiles used in following format
	 * 
	 * first line: number of tiles
	 * then names of pics
	 * number of the line after ward gives the number of the pic
	 * (line 0 after number of tiles -> tile 0 )
	 */
	public static void saveTileDefinitions(String[] tileFilePaths, String targetFilePath){
		try {
			FileWriter saveFile = null;
				saveFile = new FileWriter(targetFilePath);
			
			BufferedWriter bw = new BufferedWriter(saveFile);
			String tileNum ="";
			tileNum+=tileFilePaths.length;
			bw.write(tileNum);
			
			bw.newLine();
			
			for(String tileFilePath:tileFilePaths){
				bw.write( tileFilePath );
				bw.newLine();
			}
			
			bw.flush();
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * used by c++ export for complete export
	 * @deprecated 
	 * @param tileDirPath
	 * @param tileFileNames
	 * @param destDir
	 */
	public static void copyBmpTilesToExportDir( String tileDirPath, String[] tileFileNames, String destDir ){
		
		for(String tileFileName: tileFileNames){
			File in = new File(tileDirPath+"/"+tileFileName);
			File out = new File ( destDir+"/"+tileFileName);
			try {
				FileUtils.copyFile(in, out);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	
	public static void directSaveOfMemoryTiles(MapEditorInput mei, String dirPath) {
		for(int i=0; i<mei.tileFilePaths.length; i++){
			
			String tilePathAndName = dirPath+"/"+mei.tileFilePaths[i];
			
			ExportHelper.eraseFileIfPreExisting(tilePathAndName);
			
			ImageLoader loader = new ImageLoader();
			loader.data = new ImageData[] {mei.tiles[0][i].getImageData()};
			loader.save(tilePathAndName, SWT.IMAGE_BMP);
		}
	}

	
	public static void eraseFileIfPreExisting(String pathAndFileName){
		File pot = new File(pathAndFileName);
		if(pot.exists()){
			pot.delete();
		}
		
		
	}
	
}
